package com.sc.commons.base.dto;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sc.commons.base.utils.except.ScException;

public class BaseResponse {


    /**
     * 返回的状态码，通常来说，会和Http的状态码保持一致:200为成功 ,400到499为不可重试的错误(例如权限，参数校验失败),500到599为可重复的错误(网络波动，服务无法连接等)
     */
    private Integer resCode;

    /**
     * 返回的信息，通常只有 resCode不是200的时候，才会有用。
     */
    private String resMsg;

    public static BaseResponse ok() {
        BaseResponse baseRes = new BaseResponse();
        baseRes.setResCode(200);
        return baseRes;
    }

    public static BaseResponse ok(String message) {
        BaseResponse baseRes = new BaseResponse();
        baseRes.setResCode(200);
        baseRes.setResMsg(message);
        return baseRes;
    }

    public static BaseResponse error(Integer code, String info) {
        BaseResponse baseRes = new BaseResponse();
        baseRes.setResCode(code);
        baseRes.setResMsg(info);
        return baseRes;
    }
    public static BaseResponse error(ScException e){
        BaseResponse baseRes = new BaseResponse();
        baseRes.setResCode(e.getCode());
        baseRes.setResMsg(e.getMessage());
        return baseRes;
    }

    @JsonIgnore
    public boolean isOk(){
        if(resCode != null && resCode == 200){
            return true;
        }
        return false;
    }
    public Integer getResCode() {
        return resCode;
    }

    public void setResCode(Integer resCode) {
        this.resCode = resCode;
    }

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }
}
